package com.javasm.service.goods.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javasm.common.page.PageResult;
import com.javasm.domin.criteria.goods.ProductCriteria;
import com.javasm.domin.entity.goods.Product;
import com.javasm.domin.vo.goods.ProductVo;
import com.javasm.mapper.goods.ProductMapper;
import com.javasm.service.base.impl.BaseServiceimpl;
import com.javasm.service.goods.IProductService;
import com.javasm.transfer.goods.ProductTransfer;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author whl
 * @since 2022-01-14
 */
@Service
@Transactional
@RequiredArgsConstructor
public class ProductServiceImpl extends BaseServiceimpl<Product> implements IProductService {

    private final ProductMapper productMapper;
    private final ProductTransfer productTransfer;

    @Override
    public PageResult<ProductVo> searchPage(ProductCriteria productCriteria) {
        PageHelper.startPage(productCriteria.getCurrentPage(), productCriteria.getPageSize());
        LambdaQueryWrapper<Product> lambda = new QueryWrapper<Product>().lambda();

        if (!StringUtils.isEmpty(productCriteria.getId())) {
            lambda.eq(Product::getId, productCriteria.getId());
        }
        if (!StringUtils.isEmpty(productCriteria.getProductModel())) {
            lambda.like(Product::getProductModel, productCriteria.getProductModel());
        }
        if (!StringUtils.isEmpty(productCriteria.getProductColor())) {
            lambda.eq(Product::getProductColor, productCriteria.getProductColor());
        }
        if (!StringUtils.isEmpty(productCriteria.getProductBrand())) {
            lambda.eq(Product::getProductBrand, productCriteria.getProductBrand());
        }
        if (!StringUtils.isEmpty(productCriteria.getProductType())) {
            lambda.eq(Product::getProductType, productCriteria.getProductType());
        }
        List<Product> products = productMapper.selectList(lambda);
        PageInfo<Product> pageInfo = new PageInfo<>(products);

        return new PageResult<ProductVo>(pageInfo.getTotal(), productTransfer.toVO(products));
    }
}
